<%@ page language="java" pageEncoding="UTF-8" contentType="text/html; charset=utf-8" errorPage="/error.jsp" isELIgnored="false" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-tiles" prefix="tiles" %>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="http://www.opensymphony.com/oscache" prefix="cache" %>
<%@ taglib uri="http://atleap.blandware.com/taglib" prefix="atleap" %>
<html:xhtml />

<c:set var="thisLevel" value="${requestScope.level}" />
<atleap:message key="forum.forum.form.openForum" var="openForumMsg" />
<atleap:message key="forum.topic.form.readTopic" var="openTopicMsg" />
<atleap:iterator items="${requestScope.forums}" var="forum">
    <c:set var="filledForum" value="${requestScope.allForums[forum.id]}" />
    <atleap:isUserInRole roles="${filledForum.roles}" property="name" var="isAllowed" />
    <c:set var="forumsDrawn" scope="request" value="${requestScope.forumsDrawn + 1}" />
    <c:set var="isCurrent" value="${requestScope.currentForum != null && filledForum.id == requestScope.currentForum.id}" />
    <c:set var="isActive" value="${filledForum.active}" />
    <c:set var="sectionClass" value="${isCurrent ? 'currentForum' : (isActive ? 'activeForum' : 'inactiveForum')}" />
    <c:set var="needLink" value="${isActive && isAllowed}" />
    <tr>
        <atleap:iterator begin="1" end="${thisLevel}">
            <td class="transparentCell" rowspan="2"></td>
        </atleap:iterator>
        <td class="noRightBottomBorder" width="20">
            <img src="<atleap:rewriteUrl href="/images/theme/${appConfig['frontendCssTheme']}/folder.gif" />" border="0" align="absmiddle" width="20" height="15" />
        </td>
        <td class="noLeftBottomBorder" colspan="${maxLevel + 1 - thisLevel}">
            <div class="$sectionClass">
                <c:choose>
                    <c:when test="${needLink}">
                        <atleap:link action="/forum/item${forum.id}" title="${openForumMsg}">
                            <atleap:fieldValue valueMap="${filledForum.title}" complexSearch="true" filter="true" />
                        </atleap:link>
                    </c:when>
                    <c:otherwise>
                        <atleap:fieldValue valueMap="${filledForum.title}" complexSearch="true" filter="true" />
                    </c:otherwise>
                </c:choose>
            </div>
        </td>
        <td rowspan="2" align="center">
            <c:if test="${isAllowed}">
                <c:out value="${filledForum.numberOfTopics}" escapeXml="true" />
            </c:if>
        </td>
        <td rowspan="2" align="center">
            <c:if test="${isAllowed}">
                <c:out value="${filledForum.numberOfReplies}" escapeXml="true" />
            </c:if>
        </td>
        <td rowspan="2">
            <c:if test="${isAllowed && isActive && filledForum.lastPost != null}">
                <atleap:link action="forum/topic${filledForum.lastPost.topic.id}" title="${openTopicMsg}"><atleap:fieldValue valueMap="${filledForum.lastPost.title}" complexSearch="true" filter="true" /></atleap:link>
                (<c:out value="${filledForum.lastPost.author}" escapeXml="true" />)
                <atleap:formatDate value="${filledForum.lastPost.date}" type="datetime" />
            </c:if>
        </td>
        <td rowspan="2" align="center">
            <c:choose>
                <c:when test="${pageContext.request.remoteUser == null || !filledForum.usersSubscribed[pageContext.request.remoteUser]}">
                    <c:if test="${isAllowed && isActive}">
                        <c:set var="trackAction" value="forum/subscription/callCreate" />
                        <c:set var="trackTransaction" value="false" />
                        <c:if test="${pageContext.request.remoteUser != null}">
                            <c:set var="trackAction" value="forum/subscription/create" />
                            <c:set var="trackTransaction" value="true" />
                        </c:if>
                        <atleap:message key="forum.forum.buttons.track" var="trackTitle" />
                        <atleap:link action="${trackAction}" transaction="${trackTransaction}" title="${trackTitle}">
                            <atleap:param name="forumId" value="${forum.id}" />
                            <img src="<atleap:rewriteUrl href="/images/theme/${appConfig['frontendCssTheme']}/look.gif" />" border="0" title="${trackTitle}" alt="${trackTitle}" align="absmiddle" />
                        </atleap:link>
                    </c:if>
                </c:when>
                <c:otherwise>
                    <atleap:message key="forum.forum.buttons.untrack" var="untrackTitle" />
                    <atleap:link action="forum/subscription/delete" transaction="true" title="${untrackTitle}">
                        <atleap:param name="forumId" value="${forum.id}" />
                        <img src="<atleap:rewriteUrl href="/images/theme/${appConfig['frontendCssTheme']}/no_look.gif" />" border="0" title="${untrackTitle}" alt="${untrackTitle}" align="absmiddle" />
                    </atleap:link>
                </c:otherwise>
            </c:choose>
        </td>
    </tr>
    <tr class="minimalHeight">
        <td class="belowForumIcon"></td>
        <td class="noLeftTopBorder" colspan="${maxLevel + 1 - thisLevel}">
            ${filledForum.annotation}
        </td>
    </tr>
    <c:set var="level" scope="request" value="${thisLevel + 1}" />
    <c:set var="forums" scope="request" value="${forum.childForums}" />
    <jsp:include page="/WEB-INF/pages/forum/forumsTreeSection.jsp" />
</atleap:iterator>